package com.ycsj.goldmedalnewconcept.activity;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.ycsj.goldmedalnewconcept.bean.TeachingObjectiveResponse;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;
import com.ycsj.goldmedalnewconcept.R;

public class TeachingObjectivesActivity extends Activity {

	private TextView tv_techingobjective;
	private ImageView iv_back;
	private TextView tv_difficultpoint;
	private TextView tv_teachingtitle;
	private TextView tv_sikao;
	private String role;
	private String class_id;
	private String user_id;
	private int unit;
	private int Lesson;
	private Intent intent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_teachingobjectives);
		unit = getIntent().getIntExtra("Unit", 1);
		Lesson = getIntent().getIntExtra("Lesson", 1);
		class_id = getIntent().getStringExtra("class_id");
		user_id = getIntent().getStringExtra("user_id");
		// Log.e("test",class_id+"id");
		// Log.e("test",Lesson+"lesson");
		// Log.e("test",unit+"unit");
		role = SPUtil.getString(this, "role", "");// 获取账号角色类型
		initView();
		if ("3".equals(role)) {
			tv_sikao.setText("教师的思考");
		} else if ("2".equals(role)) {
			tv_sikao.setText("我的思考");
		} else if ("1".equals(role)) {
			tv_sikao.setVisibility(tv_sikao.GONE);
			tv_difficultpoint.setVisibility(tv_difficultpoint.GONE);
		}
		setOnListener();
		initData();
	}

	private void initView() {

		tv_techingobjective = (TextView) findViewById(R.id.tv_techingobjective);
		tv_teachingtitle = (TextView) findViewById(R.id.tv_teachingtitle);
		tv_difficultpoint = (TextView) findViewById(R.id.tv_difficultpoint);
		tv_sikao = (TextView) findViewById(R.id.tv_sikao);
		iv_back = (ImageView) findViewById(R.id.iv_back);
	}

	private void setOnListener() {
		if ("2".equals(role)) {
			tv_sikao.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					Intent intent1 = new Intent(TeachingObjectivesActivity.this,
							FansiActivity.class);
					intent1.putExtra("class_id1", class_id);
					intent1.putExtra("lesson1", Lesson);
					intent1.putExtra("unit1", unit);
					Log.e("test", class_id + "id");
					Log.e("test", Lesson + "lesson");
					Log.e("test", unit + "unit");
					startActivity(intent1);

				}
			});
		}
		if ("3".equals(role)) {

			tv_sikao.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intent2 = new Intent(
							TeachingObjectivesActivity.this,
							CheckFansiActivity.class);
					intent2.putExtra("class_id", class_id);
					intent2.putExtra("user_id",user_id);
					intent2.putExtra("Lesson",Lesson);
					intent2.putExtra("unit", unit);
					Log.e("test", class_id + "id");
					Log.e("test", Lesson + "lesson");
					Log.e("test", unit + "unit");
					Log.e("test", user_id + "user_id");
					startActivity(intent2);
				}
			});
		}

		tv_techingobjective.setMovementMethod(ScrollingMovementMethod
				.getInstance());
		iv_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		tv_difficultpoint.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String string = tv_difficultpoint.getText().toString();
				if ("重难点".equals(string)) {
					tv_difficultpoint.setText("教学目标");
					tv_teachingtitle.setText("重难点");
					OkHttpClient mOkHttpClient = new OkHttpClient();
					// 创建一个Request
					final Request request = new Request.Builder().url(
							"http://pm.moregolden.com/ycsj_count/MessageCenter?type=16&unit="
									+ unit).build();
					// new call13355556665
					Call call = mOkHttpClient.newCall(request);
					call.enqueue(callBack);
				} else if (!"重难点".equals(string)) {
					tv_difficultpoint.setText("重难点");
					tv_teachingtitle.setText("教学目标");
					OkHttpClient mOkHttpClient = new OkHttpClient();
					// 创建一个Request
					final Request request = new Request.Builder().url(
							"http://pm.moregolden.com/ycsj_count/MessageCenter?type=17&unit="
									+ unit).build();
					// new call13355556665
					Call call = mOkHttpClient.newCall(request);
					call.enqueue(callBack);
				}
			}
		});
	}

	private void initData() {
		// TODO Auto-generated method stub
		OkHttpClient mOkHttpClient = new OkHttpClient();
		// 创建一个Request
		final Request request = new Request.Builder().url(
				"http://pm.moregolden.com/ycsj_count/MessageCenter?type=17&unit="
						+ unit).build();
		// new call13355556665
		Call call = mOkHttpClient.newCall(request);
		call.enqueue(callBack);
	}

	Handler handler = new Handler();
	Callback callBack = new Callback() {

		@Override
		public void onResponse(Response arg0) throws IOException {
			String json = arg0.body().string();
			Gson gson = new Gson();
			final TeachingObjectiveResponse fromJson = gson.fromJson(json,
					TeachingObjectiveResponse.class);
			Log.e("testtips", "fromJson.content==--==" + fromJson.content);

			handler.postDelayed(new Runnable() {

				@Override
				public void run() {
					String ss = "<font size=\"4\" color=\"#ff0000\"><font ><p><b >Teaching goals:</b></p></font><p>1.Language goals:</p><div>a.Words and phrases:</div> <div>n.     umbrella ticket  number  cloakroom  daughter son  teacher  suit  school  sir</div><div>pron.    my</div><div>num.    five</div><div>b.Drills:</div> <div>1.—Is this your…?</div><div>—No, it isn't./ Yes, it is.</div> <div>2. This is not my…</div><div>c.Expressions:</div><div>My coat and my umbrella, please.</div><div>Sorry, sir.</div><p>2.Ability goals: Enable the students to know how to talk about the things they lost or found.</p></font>";

					tv_techingobjective
							.setText(Html.fromHtml(fromJson.content));
				}
			}, 100);
		}

		@Override
		public void onFailure(Request arg0, IOException arg1) {
			// TODO Auto-generated method stub

		}
	};
}
